package com.pisolution.scheduler.runtime.master;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

/**
 * 从配置文件中读取管理节点相关配置并在管理节点启动时配置管理节点.
 *
 * @author sora
 * @version 0.1.0
 */
@Component
@PropertySource(value = "classpath:master.properties")
public class MasterConfig {
    /** 管理节点交互服务对外开放端口. */
    @Value(value = "${master.port:10608}")
    private int port;

    /** 心跳上传时间间隔. */
    @Value(value = "${master.heartbeat.interval:10}")
    private int heartbeatInterval;

    /** 需要预留多少内存给主机. */
    @Value(value = "${master.memory.reserved:0.3}")
    private double reservedMemory;

    /** 主机处理器最大平均负载. */
    @Value(value = "${master.cpu.load.avg.max:-1}")
    private double maxCPULoadAvg;

    /** 用于执行系统控制指令的线程数. */
    @Value(value = "${master.scheduler.threads:100}")
    private int masterSchedulerThreads;

    /** 每一个任务提交至数据库与共享队列中的失败重试此处. */
    @Value(value = "${master.task.commit.retry-count:7}")
    private int masterTaskCommitRetryCount;

    /** 每一个任务提交至数据库与共享队列中的失败重试间隔. */
    @Value(value = "${master.task.commit.interval:1000}")
    private int masterTaskCommitInterval;

    /** 任务队列扫描器一次性取出的任务数量. */
    @Value(value = "${master.dispatch.task.num:3}")
    private int masterDispatchTaskNum;

    // -------------------------------------------------------------------------------------------------
    //  属性访问
    // -------------------------------------------------------------------------------------------------

    public int getPort() {
        return this.port;
    }

    public int getHeartbeatInterval() {
        return this.heartbeatInterval;
    }

    public double getReservedMemory() {
        return this.reservedMemory;
    }

    public double getMaxCPULoadAvg() {
        return this.maxCPULoadAvg == -1 ? Runtime.getRuntime().availableProcessors() << 1 : this.maxCPULoadAvg;
    }

    public int getMasterSchedulerThreads() {
        return this.masterSchedulerThreads;
    }

    public int getMasterTaskCommitRetryCount() {
        return this.masterTaskCommitRetryCount;
    }

    public int getMasterTaskCommitInterval() {
        return this.masterTaskCommitInterval;
    }

    public int getMasterDispatchTaskNum() {
        return this.masterDispatchTaskNum;
    }
}
